package com.ydzt.fanka.server.space.transmit;

import java.io.File;

import org.apache.log4j.Logger;

import com.ydzt.fanka.server.init.InitData;

/*
 * 食堂解包入库后 会将已经解包入库的包放入transmit文件夹
 * 此线程将transmit文件夹中的报文处理为多包
 * */
public class TransmitListenrServer extends Thread{
	static Logger logger = Logger.getLogger(TransmitListenrServer.class.getName());
	private long lastTime = 0;
	
	File dir = null;
	public void run() {
		while(true){
			int step = 0;
			try {
				Thread.sleep(5000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			File f = new File(InitData.transmitPath);
			if(f != null){
				File AllMsgTypeDir[] = f.listFiles();
				if(AllMsgTypeDir != null){
					for(int i = 0;i < AllMsgTypeDir.length;i ++){
						step += AllMsgTypeDir[i].listFiles().length;
					}
					long currentTime = System.currentTimeMillis();
					//超时读取 在超过transmit_timeout所设定的时间后 会调用MultiPackage.getPackageToTransmit()方法
					//将transmit内的报文拼接为多包放入wait_transmit文件夹
					if(currentTime - lastTime > Long.parseLong(InitData.transmit_timeout)){
						logger.info("超时 读取transmit文件夹");
						//调用此方法将单包处理为多包并放入wait_transmit文件夹
						MultiPackage.getPackageToTransmit();
						this.lastTime = currentTime;
					}
					else{
						//当在超过transmit_max_record所设定的报文数后 会调用MultiPackage.getPackageToTransmit()方法
						//将transmit内的报文拼接为多包放入wait_transmit文件夹
						if(step > Integer.parseInt(InitData.transmit_max_record)){
							//调用此方法将单包处理为多包并放入wait_transmit文件夹
							MultiPackage.getPackageToTransmit();
						}
					}
				}
			}
		}
	}
}
